package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.BuddhismDetailActivity;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.db.dao.ThemeTB;
import com.v1.video.db.dao.ThemeVideoTB;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 佛教适配器
 * 
 * @author wangfang
 * 
 */
public class BuddhismAdapter extends BaseAdapter {

	private Context mContext;
	private int mHeight;
	private int mWidth;
	private List<ThemeTB> mLstData;

	public BuddhismAdapter(Context context) {
		this.mContext = context;
		this.mWidth = Utils.computeImageWidthTo3(context);
		this.mHeight = Utils.computeImageHeightWithWidth(mWidth);
		mLstData = new ArrayList<ThemeTB>();
	}

	public List<ThemeTB> getLstData() {
		return mLstData;
	}

	public void setLstData(List<ThemeTB> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext,
					R.layout.fragment_buddhism_item, null);
			holder = new Holder();
			holder.theme = (TextView) convertView.findViewById(R.id.tv_theme);
			holder.layItem = convertView.findViewById(R.id.lay_item);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.title2 = (TextView) convertView.findViewById(R.id.tv_title2);
			holder.image2 = (ImageView) convertView
					.findViewById(R.id.iv_image2);
			holder.title3 = (TextView) convertView.findViewById(R.id.tv_title3);
			holder.image3 = (ImageView) convertView
					.findViewById(R.id.iv_image3);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final ThemeTB info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getThemeName())) {
			holder.theme.setText(info.getThemeName());
		} else {
			holder.theme.setText("");
		}
		holder.theme.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(mContext, BuddhismDetailActivity.class);
				intent.putExtra("title", info.getThemeName());
				intent.putExtra("themdId", info.getThemeID());
				intent.putExtra("imgUrl", info.getThemeImg());
				intent.putExtra("content", info.getBrief());
				mContext.startActivity(intent);
			}
		});

		android.view.ViewGroup.LayoutParams Params = holder.image
				.getLayoutParams();
		Params.width = mWidth; // 设置图片的宽度
		Params.height = mHeight;// 设置图片的高度
		holder.image.setLayoutParams(Params);
		android.view.ViewGroup.LayoutParams Params2 = holder.image2
				.getLayoutParams();
		Params2.width = mWidth; // 设置图片的宽度
		Params2.height = mHeight;// 设置图片的高度
		holder.image2.setLayoutParams(Params2);
		android.view.ViewGroup.LayoutParams Params3 = holder.image3
				.getLayoutParams();
		Params3.width = mWidth; // 设置图片的宽度
		Params3.height = mHeight;// 设置图片的高度
		holder.image3.setLayoutParams(Params3);
		if (info.getVideoList() != null && info.getVideoList().size() > 0) {
			holder.layItem.setVisibility(View.VISIBLE);
			ThemeVideoTB video = info.getVideoList().get(0);
			if (video != null) {
				if (!TextUtils.isEmpty(video.getTitle())) {
					holder.title.setText(info.getVideoList().get(0).getTitle());
				} else {
					holder.title.setText("");
				}
				ImageLoader.getInstance().displayImage(
						info.getVideoList().get(0).getPic(), holder.image,
						Constant.VIDEO_10_16_OPTION_ROUNDED);
			} else {
				holder.title.setText("");
				holder.image
						.setImageResource(R.drawable.common_place_holder_bg_16_10);
			}
			if (info.getVideoList().size() > 1) {
				holder.title2.setVisibility(View.VISIBLE);
				holder.image2.setVisibility(View.VISIBLE);
				video = info.getVideoList().get(1);
				if (video != null) {
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title2.setText(info.getVideoList().get(1)
								.getTitle());
					} else {
						holder.title2.setText("");
					}
					ImageLoader.getInstance().displayImage(
							info.getVideoList().get(1).getPic(), holder.image2,
							Constant.VIDEO_10_16_OPTION_ROUNDED);
				} else {
					holder.title2.setText("");
					holder.image2
							.setImageResource(R.drawable.common_place_holder_bg_16_10);
				}
			} else {
				holder.title2.setVisibility(View.GONE);
				holder.image2.setVisibility(View.GONE);
			}
			if (info.getVideoList().size() > 2) {
				holder.title3.setVisibility(View.VISIBLE);
				holder.image3.setVisibility(View.VISIBLE);
				video = info.getVideoList().get(2);
				if (video != null) {
					if (!TextUtils.isEmpty(video.getTitle())) {
						holder.title3.setText(info.getVideoList().get(2)
								.getTitle());
					} else {
						holder.title3.setText("");
					}
					ImageLoader.getInstance().displayImage(
							info.getVideoList().get(2).getPic(), holder.image3,
							Constant.VIDEO_10_16_OPTION_ROUNDED);
				} else {
					holder.title3.setText("");
					holder.image3
							.setImageResource(R.drawable.common_place_holder_bg_16_10);
				}
			} else {
				holder.title3.setVisibility(View.GONE);
				holder.image3.setVisibility(View.GONE);
			}
			holder.image.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					transferVideoDetail(info.getVideoList().get(0));
				}
			});
			holder.image2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					transferVideoDetail(info.getVideoList().get(1));
				}
			});
			holder.image3.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					transferVideoDetail(info.getVideoList().get(2));
				}
			});
		} else {
			holder.layItem.setVisibility(View.GONE);
		}
		return convertView;
	}

	private void transferVideoDetail(ThemeVideoTB info) {
		ScenarioVideoInfo video = new ScenarioVideoInfo(info.getVid());
		if (video != null) {
			Intent intent = new Intent();
			intent.setClass(mContext, PaikeVideoDetailActivity2.class);
			intent.putExtra("videoInfo", video);
			intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
			mContext.startActivity(intent);
		}
	}

	private class Holder {
		public TextView theme;
		public View layItem;
		public TextView title;
		public TextView title2;
		public TextView title3;
		public ImageView image;
		public ImageView image2;
		public ImageView image3;
	}

}
